use chrono::Utc;
use crate::errors::{BinanceError, Result};
use crate::rest::client::BinanceRestClient;
use crate::rest::market_types::{OrderBook, OrderBookRaw};
use crate::rest::{ExchangeInfo, ExchangeInfoRaw};

impl BinanceRestClient {
    /// 获取订单簿深度（支持限价）
    pub async fn get_order_book(
        &self,
        symbol: &str,
        limit: Option<u32>, // 默认 500
    ) -> Result<OrderBook> {
        let mut query = format!("symbol={}", symbol);
        if let Some(limit_val) = limit {
            query += &format!("&limit={}", limit_val);
        }
        let url = format!("/fapi/v1/depth?{}", query);

        let res = self.get(&*url, false).await?;

        let parsed: OrderBookRaw = serde_json::from_str(&res)?;
        let mut model = parsed.to_model().ok_or_else(|| BinanceError::Other("convert failed.".into()))?;
        model.symbol = symbol.parse().unwrap();
        Ok(model)

    }

    /// 获取交易所所有交易规则和合约信息
    pub async fn get_exchange_info(&self) -> Result<ExchangeInfo> {
        let res = self.get("/fapi/v1/exchangeInfo", false).await?;
        let raw: ExchangeInfoRaw = serde_json::from_str(&res)?;
        Ok(raw.to_model())
    }

}
